#include "FuzzyRulesForm.h"

// ----------------------------------------------------------------------
CFuzzyRulesForm::CFuzzyRulesForm(FuzzyTableModel* pTblModel /* = 0*/, QGraphicsScene* pGS /* = 0*/, QWidget * parent/* = 0 */)
        : QWidget(parent, Qt::Tool)
        , m_table(0)
        , mpScene(0)
        , mpView(0)
        , mpBtnOk(0)
{
    //setObjectName(QString::fromUtf8("FuzzyTable"));
    //QModelIndex index = m_table.index(0,0);
    if (pTblModel != 0)
    {
        m_table = pTblModel;
    }
    else
    {
        m_table = pTblModel; //new FuzzyTableModel(200,200);
    }
    
    if (pGS != 0)
    {
    	mpScene = pGS;//ashvets: should be corrected
    }
    else
    {
    	mpScene = new QGraphicsScene(QRectF(-100, -100, 910, 1100));
    }
    drawAxes();
    drawGraph();
    mpView = new QGraphicsView(mpScene,this);
    mpView->setRenderHint(QPainter::Antialiasing, true);
    
    
    //Buttons setup
    mpBtnOk = new QPushButton("&Ok");

    //Layout setup
    QVBoxLayout* pvbxLayout = new QVBoxLayout;    
    pvbxLayout->addWidget(mpView);
    pvbxLayout->addWidget(mpBtnOk);
    setLayout(pvbxLayout);
    
    qDebug() << (m_table->data(m_table->index(3,3),Qt::DisplayRole)).toInt();
}

// ----------------------------------------------------------------------
CFuzzyRulesForm::~CFuzzyRulesForm()
{
	if (mpBtnOk != 0)
	{
		delete mpBtnOk;
		mpBtnOk = 0;
	}
	
	if (mpScene != 0)
	{
		delete mpScene;
		mpScene = 0;
	}
	
	if (mpView != 0)
	{
		delete mpView;
		mpView = 0;
	}
}

// ----------------------------------------------------------------------
void CFuzzyRulesForm::slotLoad()
{
    QString str = QFileDialog::getOpenFileName();
    if (str.isEmpty())
    {
        return;
    }

    QFile file(str);
    if (file.open(QIODevice::ReadOnly))
    {
        QDataStream stream(&file);
        stream.setVersion(QDataStream::Qt_4_3);

        int r = 0; // rows amount
        int c = 0; // columns amount
        stream >> r >> c;
        m_table = new FuzzyTableModel(r,c);
        QVariant var;
        for (int i=0; i<r; i++)
            for (int j=0; j<c; j++)
            {
                stream >> var;
                m_table->setData(m_table->index(i,j),var,Qt::EditRole);
            }
        file.close();

        m_strFileName = str;
        emit changeWindowTitle(m_strFileName);
    }

    //setModel(m_table);
    //show();
}

// ----------------------------------------------------------------------
void CFuzzyRulesForm::slotSave()
{
    if (m_strFileName.isEmpty())
    {
        slotSaveAs();
        return;
    }

    QFile file(m_strFileName);
    if (file.open(QIODevice::WriteOnly))
    {
        QDataStream stream(&file);
        stream.setVersion(QDataStream::Qt_4_3);

        //m_table.setData(m_table.index(1,1),QVariant("fuck"),Qt::EditRole);
        QModelIndex index;
        int r = m_table->rowCount(index);	// rows amount
        int c = m_table->columnCount(index);	// columns amount
        stream << r << c;
        for (int i=0; i<r; i++)
            for (int j=0; j<c; j++)
                stream << m_table->data(m_table->index(i,j),Qt::DisplayRole);
        file.close();
        emit changeWindowTitle(m_strFileName);
    }
}

// ----------------------------------------------------------------------
void CFuzzyRulesForm::slotSaveAs()
{
    QString str = QFileDialog::getSaveFileName(0, m_strFileName);
    if (!str.isEmpty())
    {
        m_strFileName = str;
        slotSave();
    }
}

// ----------------------------------------------------------------------
/*void CFuzzyRulesForm::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    painter.translate(0, this->height());
    painter.setPen(QPen(Qt::red, 3, Qt::SolidLine));
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.drawLine(QPointF(10,10),QPointF(90,90));
    
}*/

void CFuzzyRulesForm::drawAxes()
{
	//Horizontal axe
	mpScene->setSceneRect(-50, 1050, 1100, -1100);
	mpScene->addLine(0, 1000, 100, 1000, QPen(Qt::gray, 1));
	mpScene->addLine(0, 900, 500, 900, QPen(Qt::gray, 1));
	mpScene->addLine(50, 1000, 50, 1010, QPen(Qt::gray, 1));
	
}

void CFuzzyRulesForm::drawGraph()
{
	mpScene->addLine(0, 900, 100, 0, QPen(Qt::black, 2));
}
